//
//  ZJHomeViewController.m
//  ZiJinLian
//
//  Created by lzy on 2017/3/10.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "ZJHomeViewController.h"
#import "ZJTipsBtn.h"
#import "ZJMeHeadView.h"
#import "ShopsCollectionViewCell.h"
#import "CollectionViewHeaderView.h"
#import "FirstHeadCollectionReusableView.h"
#import "SecondHeadCollectionReusableView.h"
#import "SupplicationViewController.h"
#import "ApplyViewController.h"
#import "EnterViewController.h"
#import "TempleViewController.h"
#import "ShopListsViewController.h"
#import "MessageViewController.h"
#import "CityViewController.h"
#import "homeModel.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "ZJBaseNavControllerViewController.h"
#import "LogisViewController.h"
#import "LoadMaskHelper.h"
//商品详情
//H5页面

//测试跳转的控制器
#import "ShopDetailViewController.h"
#import "ShopListsViewController.h"
#import "MyOrderViewController.h"
#import "MyOrderListViewController.h"
@interface ZJHomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,FirstHeadReusableViewDelegate,SecondHeadReusableViewDelegate,CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager * locationManager;
@property (nonatomic, strong) UICollectionView  *collectionView;
@property (nonatomic,strong)  homeModel*headModel;
@property (nonatomic, strong) SDCycleScrollView * cycleScrollerView;
@property (nonatomic, strong) SDCycleScrollView * sectionScrollerView;
@property (nonatomic, strong) UIButton * sectionBtn;
@property (strong, nonatomic) UIButton     * topBtn;
@property (nonatomic, strong) FirstHeadCollectionReusableView *Firstview;
@property (nonatomic, strong) MYLabel * HeadLabel;
@property (nonatomic, strong) UILabel * leftlabel;
@property (nonatomic, strong) NSArray *firstArray;
@property (nonatomic, strong) NSArray *groupArray;
@property (nonatomic, strong) NSArray *firstAdArray;
@property (nonatomic, strong) NSArray *secondArray;
@property (nonatomic, strong) NSArray *itemsArray;
@property (nonatomic, strong) UIImageView*msgImg;
@end

@implementation ZJHomeViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    _isLogin=[ZJUserDefaultsTools isLogin];
    self.navigationController.navigationBar.hidden=NO;
    
    [self setupUnreadMessageCount];
    //[self checkVersionRequest];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // 主页蒙版
    // 主页蒙版
    NSLog(@"----%f",self.view.frame.size.width);
    [self loadMainData];
    //[self loadAddressData];
    [self startLocation];
    [self.view addSubview:self.collectionView];
    [self creatNavUI];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(InfoNotificationAction) name:@"unReadInfo" object:nil];
    //避免数据已经存在
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(maskView:) name:@"InfoNotification" object:nil];
    //其他设备登录通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitAccount) name:@"lginOther" object:nil];
    
    
    // Do any additional setup after loading the view.
}
#pragma mark 退出账户
-(void)exitAccount{
    UIAlertView* alter =[[UIAlertView alloc]initWithTitle:MyString(@"该账户已在别的设备登录,请重新登录!") message:nil delegate:self cancelButtonTitle:MyString(@"确定") otherButtonTitles:MyString(@"取消"), nil];
    [alter show];
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
    if (buttonIndex==0) {
        EMError *error = [[EMClient sharedClient] logout:YES];
        if (!error) {
            NSLog(@"退出成功");
        }
        [ZJUserDefaultsTools clearWithoutAvatar];
        
        ZJLoginViewController *controller = [[ZJLoginViewController alloc] init];
        
        ZJBaseNavControllerViewController*
        nav=[[ZJBaseNavControllerViewController alloc]initWithRootViewController:controller];
        //controller.modality=NO;
        [self presentViewController:nav animated:YES completion:nil];
    }
}
-(void)maskView:(NSNotification *)info{
    if (self.groupArray.count!=0&&self.groupArray!=nil&&[info.userInfo[@"info"] isEqualToString:@"shadown"]) {
        [LoadMaskHelper showMaskWithType:HomePage onView:self.view delay:0.01];
    }
    
}
#pragma mark -----去除表头悬停效果
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView == self.collectionView) {
        //CGFloat sectionHeaderHeight = 50;
        if (scrollView.contentOffset.y>=MainH*1.5) {
            self.topBtn.hidden=NO;
        }else{
            self.topBtn.hidden=YES;
        }
    }
    
}
-(void)backTopClick{
    [self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
}

-(void)InfoNotificationAction{
     self.msgImg.image=[UIImage imageNamed:@"icon_message_reminder"];
    //NSLog(@"消息啊啊啊啊");
}
-(void)creatNavUI{
    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 44)];
    self.msgImg=[[UIImageView alloc]initWithFrame:CGRectMake(24, 6, 16, 16)];
    self.msgImg.image=[UIImage imageNamed:@"icon_message_nor"];
    [testView addSubview:self.msgImg];
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(4, 24, 40, 15)];
    label.text=MyString(@"消息");
    label.textColor=[UIColor whiteColor];
    label.font=FontAndStyle12;
    label.textAlignment=2;
    [testView addSubview:label];
    UIButton *buttn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 44)];
    [buttn addTarget:self action:@selector(showMessageClick) forControlEvents:UIControlEventTouchUpInside];
    [testView addSubview:buttn];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:testView];
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 44)];
    self.leftlabel=[[UILabel alloc]initWithFrame:CGRectMake(-5, 24, 40, 15)];
    self.leftlabel.text=MyString(@"南通");
    self.leftlabel.textColor=[UIColor whiteColor];
    self.leftlabel.font=FontAndStyle12;
    self.leftlabel.textAlignment=1;
    [leftView addSubview:self.leftlabel];
    UIImageView*leftImg=[[UIImageView alloc]initWithFrame:CGRectMake(self.leftlabel.bsg_centerX-6, 6, 12, 16)];
    leftImg.image=[UIImage imageNamed:@"icon_location"];
    [leftView addSubview:leftImg];
    UIButton *leftbuttn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 44)];
    [leftbuttn addTarget:self action:@selector(showaddressClick) forControlEvents:UIControlEventTouchUpInside];
    [leftView addSubview:leftbuttn];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftView];
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MainW-120, 30)];//allocate titleView
    //UIColor *color =  self.navigationController.navigationBar.tintColor;
    [titleView setBackgroundColor:[UIColor clearColor]];
    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, MainW-120, 30)];
    [btn addTarget:self action:@selector(searchClick) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:[ZJUITools customSearchbar]];
    [titleView addSubview:btn];
    //Set to titleView
    self.navigationItem.titleView = titleView;
    self.topBtn=[[UIButton alloc]init];
    self.topBtn.frame= CGRectMake(MainW-50, MainH-64-100, 40, 40);
    self.topBtn.hidden=YES;
    [self.topBtn setBackgroundImage:[UIImage imageNamed:@"icon_top"] forState:UIControlStateNormal];
    [self.topBtn addTarget:self action:@selector(backTopClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.topBtn];
    [self.topBtn bringSubviewToFront:self.collectionView];
    
}
#pragma mark ------创建搜索按钮
-(void)searchClick{
    ShopListsViewController*vc=[[ShopListsViewController alloc]init];
    vc.origin=2;
    [self.navigationController pushViewController:vc animated:YES];
    //NSLog(@"搜索");
}
#pragma mark ------创建导航内容
-(void)showMessageClick{
    if (_isLogin) {
        EaseConversationListViewController *chatController = [[EaseConversationListViewController alloc]init];
        chatController.title=MyString(@"消息");
        [self.navigationController pushViewController:chatController animated:YES];
    }else{
        ZJLoginViewController *controller = [[ZJLoginViewController alloc] init];
        ZJBaseNavControllerViewController* nav=[[ZJBaseNavControllerViewController alloc]initWithRootViewController:controller];
        controller.modality=NO;
        [self presentViewController:nav animated:YES completion:nil];
    }
    
    //NSLog(@"消息");
}
-(void)showaddressClick{
    
//    CityViewController *controller = [[CityViewController alloc] init];
//    controller.currentCityString = self.leftlabel.text;
//    controller.selectString = ^(NSString *string){
//        self.leftlabel.text = string;
//    };
//    [self presentViewController:controller animated:YES completion:nil];
    //NSLog(@"地址");
}

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
        //设置滚动方向
        [flowlayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        //左右间距
        flowlayout.minimumInteritemSpacing =5;
        //上下间距
        flowlayout.minimumLineSpacing =5 ;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, MainW, MainH-64-49) collectionViewLayout:flowlayout];
        _collectionView.backgroundColor = Gray_Color;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        //注册cell
        [_collectionView registerNib:[UINib nibWithNibName:@"ShopsCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ShopsCell"];
        //注册分区头标题
        [_collectionView registerClass:[FirstHeadCollectionReusableView class]
            forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                   withReuseIdentifier:@"FirstHeadCollectionReusableView"];
        [_collectionView registerClass:[SecondHeadCollectionReusableView class]
            forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                   withReuseIdentifier:@"SecondHeadCollectionReusableView"];
        _collectionView.mj_header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(topLoadRefresh)];
    }
    return _collectionView;
}
-(void)topLoadRefresh{
    [self loadMainData];
    //NSLog(@"刷新");
    [_collectionView.mj_header endRefreshing];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return CGSizeMake(MainW,253+MainW*0.8+(((MainW-15)/3)*(self.firstArray.count/2)));
    }else{
        return CGSizeMake(MainW,53+MainW*0.4);
    }
    
}
#pragma mark - UITableView DataSource Delegate



- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.groupArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{   self.headModel=self.groupArray[section];
    NSArray *arry=self.headModel.items;
    return arry.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ShopsCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShopsCell" forIndexPath:indexPath];
    self.headModel=self.groupArray[indexPath.section];
    NSArray *arry=self.headModel.items;
        int w=(MainW-5)*1.5;
        int h=(MainW-5)*1.5;
        [cell.headImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?x-oss-process=image/resize,m_fixed,h_%d,w_%d",arry[indexPath.row][@"imageUrl"],h,w]] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    cell.titleLabel.text=[NSString stringWithFormat:@"%@",arry[indexPath.row][@"title"]];
    cell.moneyLabel.text=[NSString stringWithFormat:@"¥ %@",arry[indexPath.row][@"fixedPrice"]];
    cell.stateLabel.text=[NSString stringWithFormat:@"%@: %@件",MyString(@"月销量"),arry[indexPath.row][@"volume"]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.headModel=self.groupArray[indexPath.section];
    NSArray *arry=self.headModel.items;
    ShopDetailViewController*vc=[[ShopDetailViewController alloc]init];
    vc.spuId=arry[indexPath.row][@"skuId"];
    [self.navigationController pushViewController:vc animated:YES];
    ////NSLog(@"商品ID----------%@级",arry[indexPath.row][@"spuId"]);
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=[ZJUITools getHeightByWidth:self.view.frame.size.width-20 title:@"释迦摩尼佛像(文殊菩萨)" font:FontAndStyle14] + (MainW-15)/2+35;
    return CGSizeMake((MainW-5)/2, height);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 0, 5, 0);//分别为上、左、下、右
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        NSString *reuseIdentifier;
        if ([kind isEqualToString:UICollectionElementKindSectionHeader])
        { // header
            reuseIdentifier = @"FirstHeadCollectionReusableView";
        }

            self.Firstview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                                withReuseIdentifier:reuseIdentifier
                                                                                       forIndexPath:indexPath];
        if ([kind isEqualToString:UICollectionElementKindSectionHeader])
        {
            NSMutableArray*array=[[NSMutableArray alloc]init];
            for (NSDictionary * dic in self.firstAdArray) {
                NSString * pic = [NSString stringWithFormat:@"%@",dic[@"imageUrl"]];
                [array addObject:pic];
            }
            self.headModel=self.groupArray[indexPath.section];
            self.Firstview.nameLabel.text=self.headModel.title;
            self.Firstview.smallTitle.text=self.headModel.desc;
            [self.Firstview.iconImag sd_setImageWithURL:[NSURL URLWithString:self.headModel.icon] placeholderImage:[UIImage imageNamed:@""]];
            self.Firstview.delegate=self;
            self.Firstview.cycleScrollerView.imageURLStringsGroup = array;  // 网络图片
            //self.firstArray=[NSArray arrayWithObjects:@"1",@"1",@"1",@"1", nil];
            NSMutableArray*Array=[[NSMutableArray alloc]init];
            for (NSDictionary*dic in self.headModel.adverts) {
                NSString * pic = [NSString stringWithFormat:@"%@",dic[@"imageUrl"]];
                [Array addObject:pic];
            }
            if (self.Firstview.sectionScrollerView==nil) {
                [self.Firstview setGroupView:self.firstArray NSArray:Array];
            }
            
        }

       
        return self.Firstview;
    }else{
        NSString *reuseIdentifier;
        if ([kind isEqualToString:UICollectionElementKindSectionHeader])
        { // header
            reuseIdentifier = @"SecondHeadCollectionReusableView";
        }
        SecondHeadCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                            withReuseIdentifier:reuseIdentifier
                                                                                   forIndexPath:indexPath];
        view.delegate=self;
        if ([kind isEqualToString:UICollectionElementKindSectionHeader])
        {
            self.headModel=self.groupArray[indexPath.section];
            view.nameLabel.text=self.headModel.title;
            view.smallTitle.text=self.headModel.desc;
            [view.iconImag sd_setImageWithURL:[NSURL URLWithString:self.headModel.icon] placeholderImage:[UIImage imageNamed:@""]];
            NSMutableArray*Array=[[NSMutableArray alloc]init];
            for (NSDictionary*dic in self.headModel.adverts) {
                NSString * pic = [NSString stringWithFormat:@"%@",dic[@"imageUrl"]];
                [Array addObject:pic];
            }
            view.sectionScrollerView.imageURLStringsGroup=Array;
            view.sectionScrollerView.tag=101+indexPath.section;
            view.sectionBtn.tag=800+indexPath.section;
        }
        return view;

    }
}
#pragma mark ------ cycleScrollViewdelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    if (cycleScrollView.tag==100) {

        ////NSLog(@"顶部的轮播%@",self.firstAdArray[index][@"type"]);
        switch ([self.firstAdArray[index][@"advertType"] intValue]) {
            case 1:
            {
                ShopDetailViewController*vc=[[ShopDetailViewController alloc]init];
                vc.spuId=self.firstAdArray[index][@"linkId"];
                [self.navigationController pushViewController:vc animated:YES];
                NSLog(@"跳转商品");
            }
                break;
            case 2:
            {
                WelcomViewController*vc=[[WelcomViewController alloc]init];
                vc.reLastURL=[NSString stringWithFormat:@"%@",self.firstAdArray[index][@"linkUrl"]];
                NSLog(@"跳转网页");
            }
                break;
            case 3:
            {
                ShopListsViewController*vc=[[ShopListsViewController alloc]init];
                vc.ids=self.firstAdArray[index][@"linkId"];
                [self.navigationController pushViewController:vc animated:YES];
                NSLog(@"跳转分类");
            }
                break;
                
                
            default:
                break;
        }

    }else if (cycleScrollView.tag==101){
        self.headModel=self.groupArray[cycleScrollView.tag-101];
        NSArray *arry=self.headModel.adverts;
        switch ([arry[index][@"advertType"] intValue]) {
            case 1:
            {
                ShopDetailViewController*vc=[[ShopDetailViewController alloc]init];
                vc.spuId=arry[index][@"linkId"];
                [self.navigationController pushViewController:vc animated:YES];
                NSLog(@"跳转商品");
            }
                break;
            case 2:
            {
                WelcomViewController*vc=[[WelcomViewController alloc]init];
                vc.reLastURL=[NSString stringWithFormat:@"%@",arry[index][@"linkUrl"]];
                NSLog(@"跳转网页");
            }
                break;
            case 3:
            {
                ShopListsViewController*vc=[[ShopListsViewController alloc]init];
                vc.ids=arry[index][@"linkId"];
                [self.navigationController pushViewController:vc animated:YES];
                NSLog(@"跳转分类");
            }
                break;

                
            default:
                break;
        }
        //NSLog(@"一区的轮播%@",arry[index][@"type"]);
    }else{
        self.headModel=self.groupArray[cycleScrollView.tag-101];
        NSArray *arry=self.headModel.adverts;
        switch ([arry[index][@"advertType"] intValue]) {
            case 1:
            {
                ShopDetailViewController*vc=[[ShopDetailViewController alloc]init];
                vc.spuId=arry[index][@"linkId"];
                [self.navigationController pushViewController:vc animated:YES];
                NSLog(@"跳转商品");
            }
                break;
            case 2:
            {
                WelcomViewController*vc=[[WelcomViewController alloc]init];
                vc.reLastURL=[NSString stringWithFormat:@"%@",arry[index][@"linkUrl"]];
                NSLog(@"跳转网页");
            }
                break;
            case 3:
            {
                ShopListsViewController*vc=[[ShopListsViewController alloc]init];
                vc.ids=arry[index][@"linkId"];
                [self.navigationController pushViewController:vc animated:YES];
                NSLog(@"跳转分类");
            }
                break;
            default:
            break;
        }
    }
}
#pragma mark ------FirstHeadReusableViewDelegate
//区头的点击事件
-(void)sectionHeadClick:(UIButton *)sender{
    //if (sender.tag==800) {
        self.headModel=self.groupArray[sender.tag-800];
        NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
        ShopListsViewController*vc=[[ShopListsViewController alloc]init];
        dic[@"cates"]=self.headModel.cates;
        dic[@"options"]=self.headModel.options;
        dic[@"sortType"]=self.headModel.sortType;
        dic[@"sortMethod"]=self.headModel.sortMethod;
        vc.dic=dic;
        vc.origin=3;
        [self.navigationController pushViewController:vc animated:YES];

}
//四个图片的点击事件
-(void)fourBtnClick:(UIButton *)sender{
    
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    ShopListsViewController*vc=[[ShopListsViewController alloc]init];
    dic[@"cates"]=self.firstArray[sender.tag-500][@"cates"];
//    dic[@"options"]=self.headModel.options;
//    dic[@"sortType"]=self.headModel.sortType;
//    dic[@"sortMethod"]=self.headModel.sortMethod;
    vc.dic=dic;
    vc.origin=3;
    [self.navigationController pushViewController:vc animated:YES];
}
//顶部6个按钮的时间
//-(void)creatView{
//    UILabel*label=[[UILabel alloc]init];
//    label.text=self.firstArray[]
//}
-(void)topBtnClick:(FL_Button *)sender{
//    [self showToast:@"暂未开放"];
//    return ;
    switch (sender.tag) {
        case 1000:
        {
            [self showToast:MyString(@"未开放")];
//            ShopListsViewController*vc=[[ShopListsViewController alloc]init];
//            [self.navigationController pushViewController:vc animated:YES];
            //NSLog(@"养生");
        }
            break;
        case 1001:
        {
            //[self showToast:MyString(@"未开放")];
            if ([ZJUserDefaultsTools isLogin]) {
                SupplicationViewController*vc=[[SupplicationViewController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                ZJLoginViewController *controller = [[ZJLoginViewController alloc] init];
                ZJBaseNavControllerViewController* nav=[[ZJBaseNavControllerViewController alloc]initWithRootViewController:controller];
                controller.modality=NO;
                [self presentViewController:nav animated:YES completion:nil];
            }
            
            
             //NSLog(@"祈愿");
        }
            break;
        case 1002:
        {
            [self showToast:MyString(@"未开放")];
             //NSLog(@"供养");
        }
            break;
        case 1003:
        {
            if ([ZJUserDefaultsTools isLogin]) {
                ApplyViewController*vc=[[ApplyViewController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                vc.title=MyString(@"经销商申请");
            }else{
                ZJLoginViewController *controller = [[ZJLoginViewController alloc] init];
                ZJBaseNavControllerViewController* nav=[[ZJBaseNavControllerViewController alloc]initWithRootViewController:controller];
                controller.modality=NO;
                [self presentViewController:nav animated:YES completion:nil];
            }
            
             //NSLog(@"经销商申请");
        }
            break;
        case 1004:
        {
            [self showToast:MyString(@"未开放")];
//            EnterViewController*vc=[[EnterViewController alloc]init];
//            [self.navigationController pushViewController:vc animated:YES];
//            vc.title=@"入驻申请";
             //NSLog(@"入驻申请");
        }
            break;
        case 1005:
        {
            [self showToast:MyString(@"未开放")];
            return;
//            TempleViewController*vc=[[TempleViewController alloc]init];
//            [self.navigationController pushViewController:vc animated:YES];
//            vc.title=@"寺院洽谈";
             //NSLog(@"寺院洽谈");
        }
            break;
            
        default:
            break;
    }
}
-(void)loadMainData{
    self.isShowDiag=NO;
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    [self doHttpRequestWithApi:API_HOMESHOP_LIST method:GET dic:dic successBlock:^(NSDictionary *dic) {
        ////NSLog(@"%@",dic);
        self.firstArray=dic[@"data"][@"parents"];
        self.firstAdArray=dic[@"data"][@"adverts"];
        self.groupArray=[homeModel mj_objectArrayWithKeyValuesArray:dic[@"data"][@"groups"]];
        [self.collectionView reloadData];
    }];
}
//定位
-(void)startLocation
{
    if([CLLocationManager locationServicesEnabled])
    {
        // 启动位置更新
        // 开启位置更新需要与服务器进行轮询所以会比较耗电，在不需要时用stopUpdatingLocation方法关闭;
        _locationManager = [[CLLocationManager alloc] init];
        //设置定位的精度
        [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        _locationManager.distanceFilter = 10.0f;
        _locationManager.delegate = self;
        if ([[[UIDevice currentDevice] systemVersion] floatValue] > 8.0)
        {
            [_locationManager requestAlwaysAuthorization];
            [_locationManager requestWhenInUseAuthorization];
        }
        
        //开始实时定位
        [_locationManager startUpdatingLocation];
    }else {
        //NSLog(@"请开启定位功能！");
    }
}
//代理,定位代理经纬度回调
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    CLLocation * newLoaction = locations[0];
    CLLocationCoordinate2D oCoordinate = newLoaction.coordinate;
    NSLog(@"旧的经度：%f，旧的维度：%f",oCoordinate.longitude,oCoordinate.latitude);

    [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%f",oCoordinate.longitude] forKey:@"longitude"];
    [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%f",oCoordinate.latitude] forKey:@"latitude"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self.locationManager stopUpdatingLocation];
    //创建地理位置解码编码器对象
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
    [geoCoder reverseGeocodeLocation:newLoaction completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        for (CLPlacemark * place in placemarks) {
            //            NSDictionary * location = [place addressDictionary];
            //            //NSLog(@"国家：%@",[location objectForKey:@"Country"]);
            //            //NSLog(@"城市：%@",[location objectForKey:@"State"]);
            //            //NSLog(@"区：%@",[location objectForKey:@"SubLocality"]);
            
            //NSLog(@"位置：%@",place.name);
            self.leftlabel.text = place.locality;
//                        //NSLog(@"国家：%@",place.country);
//                        //NSLog(@"城市：%@",place.locality);
//                        //NSLog(@"区：%@",place.subLocality);
//                        //NSLog(@"街道：%@",place.thoroughfare);
//                        //NSLog(@"子街道：%@",place.subThoroughfare);
        }
    }];
}
-(void)loadAddressData{
    [self doHttpRequestWithApi:@"/api/common/area" method:GET dic:nil successBlock:^(NSDictionary *dic) {
        NSLog(@"地址数据源:%@",dic);
    }];
}
-(void)setupUnreadMessageCount
{
    NSArray *conversations = [[EMClient sharedClient].chatManager getAllConversations];
    NSInteger unreadCount = 0;
    for (EMConversation *conversation in conversations) {
        unreadCount += conversation.unreadMessagesCount;
    }
    NSLog(@"未读消息%ld",(long)unreadCount);
    if (unreadCount==0) {
       self.msgImg.image=[UIImage imageNamed:@"icon_message_nor"];
    }else{
        //2017.6.28修改
       self.msgImg.image=[UIImage imageNamed:@"icon_message_reminder"];
    }
    
}
-(void)checkVersionRequest{
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    [self doHttpRequestWithApi:CHECKVERSION method:GET dic:dic successBlock:^(NSDictionary *dic) {
        NSLog(@"----%@",dic[@"content"]);
    }];
}
@end
